2815. 数组中的最大数对和
为保证权益,题目请参考 2815. 数组中的最大数对和(From LeetCode).
解决方案1
CPP
C++
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
int getMaxN(int num)
{
int ans = 0;
while (num > 0)
{
ans = max(ans, num % 10);
num = num / 10;
}
return ans;
}
class Solution
{
public:
int maxSum(vector<int> &nums)
{
sort(nums.begin(), nums.end(), greater<int>());
vector<int> maxNums(nums.size());
for (int i = 0; i < nums.size(); i++)
{
maxNums[i] = getMaxN(nums[i]);
}
int ans = -1;
for (int i = 0; i < nums.size(); i++)
{
if(nums[i] * 2 < ans)
{
break;
}
for (int j = i + 1; j < nums.size(); j++)
{
if (maxNums[i] != maxNums[j])
{
continue;
}
ans = max(ans, nums[i] + nums[j]);
break;
}
}
return ans;
}
};
int main()
{
Solution so;
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57